package com.ekang.admin.config;

import com.ekang.admin.common.BaseResult;
import com.google.gson.Gson;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zcz on 2017/11/6.
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    Gson gson = new Gson();

    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<BaseResult> defaultErrorHandler(HttpServletRequest request,Exception e) throws Exception {
//        String requestURI = request.getRequestURI();
//        if (requestURI.endsWith(".html")) {
//            ModelAndView mav = new ModelAndView();
//            mav.addObject("exception", exception);
//            mav.addObject("url", requestURI);
//            mav.setViewName("error");
//            return mav;
//        } else {
////            AdminResp errorResp = new AdminResp(exception.getCode(), exception.getMessage());
////            response.getWriter().write(gson.toJson(errorResp));
////            response.getWriter().flush();
//            return null;
//        }

        BaseResult baseResult = new BaseResult();
        baseResult.setCode("500");
        baseResult.setMessage(e.getMessage());
        return new ResponseEntity(baseResult, null, HttpStatus.OK);

    }
}
